#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        main.py
# Author:      KuKei
# Create Date: 2012-05-29
# Description: 入口页面视图
# Modify Date: 2012-05-29

from sd.cms.views import BaseView
from sd.cms.lib.decorator import powerLevel, cachePage
from sd.cms.lib import Params, Pager
from sd.cms.forms.appForm import GuestBookForm
from sd.cms.models.appModel import GuestBook
from django.utils.safestring import mark_safe 

class GBookView(BaseView):
    def add(self, **args):
        gbookForm = GuestBookForm()
        if self.request.POST:
            gbookForm = GuestBookForm(self)
            if gbookForm.is_valid():
                formData = gbookForm.cleaned_data   
                model = GuestBook().initData(formData)
                if self.power['isLogin'] and self.power['user'].username == formData['authorname']:
                    model.author = self.power['user'].id
                if self.request.META.has_key('HTTP_X_FORWARDED_FOR'):  
                    model.ip = self.request.META['HTTP_X_FORWARDED_FOR']  
                else:  
                    model.ip = self.request.META['REMOTE_ADDR']  
                model.save()
                self.result.flag = True
                self.result.msg = "留言成功！"
                self.viewData = {'etype':'gbmessage', 'e':model, 'theme':self.params.DEFAULTTHEME}
                renderHtml = self.render('/snippet/message.html')
                self.result.html = renderHtml._container[0]
            else:
                self.result.flag = False
                self.result.msg = gbookForm.GetErrors()
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            return self.redirect('/guestbook.html')
    
    def delete(self, **args):
        if self.request.POST:
            GuestBook.objects.get(id=int(args['id'])).delete()
            self.result.flag = True
            self.result.msg = "删除成功！"
        else:
            self.result.flag = False
            self.result.msg = "请求方式错误！"
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            return self.redirect('/guestbook.html')
    
    def listView(self, **args):
        pager = Pager(self.request)
        pager.totalSize = GuestBook.objects.filter(isdisplay=True).count()
        listGBooks = list(GuestBook.objects.filter(isdisplay=True).order_by('-crtdate')[pager.startSize:pager.getEndSize()])
        gbookForm = GuestBookForm()
        self.viewData['gbookForm'] = gbookForm
        self.viewData['listGBooks'] = listGBooks
        self.viewData['pager'] = pager.getPageList()
        return self.render('/guestbook.html')
    
    def reply(self, **args):
        if self.request.POST:
            guestbook = GuestBook.objects.get(id=int(self.request.POST['_id']))
            guestbook.reply = self.request.POST['reply']
            guestbook.save()
            self.result.flag = True
            self.result.msg = "回复成功！"
            self.viewData = {'etype':'gbreply', 'e':guestbook}
            renderHtml = self.render('/snippet/message.html')
            self.result.html = renderHtml._container[0]
        else:
            self.result.flag = False
            self.result.msg = "请求方式错误！"
            
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            return self.redirect('/guestbook.html')
    
def action(request, **args):
    return apply(GBookView(request).action, (), args)